package com.csun.cmny.provider.device.csun.sleep;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.mqtt.entity.MsgUpdateVer;
import com.csun.cmny.provider.dao.DmcCsunSleepCmdHiMapper;
import com.csun.cmny.provider.dao.DmcCsunSleepCmdMapper;
import com.csun.cmny.provider.device.csun.sleep.entity.MsgSendData;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.vo.csunversion.VersionSetVo;
import com.csun.cmny.provider.service.DmcCsunVersionSetDeviceService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.utils.WatchUtils;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020/3/31 8:58
 * @Description: the class sleep message sender
 */
@Slf4j
@Component
public class SleepMessageSender {

    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private SleepMessageGateway sleepMessageGateway;
    @Resource
    private DmcCsunSleepCmdMapper dmcCsunSleepCmdMapper;
    @Resource
    private DmcCsunSleepCmdHiMapper dmcCsunSleepCmdHiMapper;
    @Resource
    private DmcCsunVersionSetDeviceService dmcCsunVersionSetDeviceService;

    /**
     * 发送版本升级指令
     *
     * @param dmcDeviceList
     * @param dmcCsunVersionFile
     */
    public void sendUpdateVerMsg(List<DmcDevice> dmcDeviceList, DmcCsunVersionFile dmcCsunVersionFile, String fileUrl) {

        int qos = 0;    // 消息质量

        for (DmcDevice dmcDevice : dmcDeviceList) {

            MsgUpdateVer msgUpdateVer = new MsgUpdateVer();
            msgUpdateVer.setFileType(dmcCsunVersionFile.getFileType() + "");
            msgUpdateVer.setFileVersion(dmcCsunVersionFile.getFileVersion());
            msgUpdateVer.setLength(dmcCsunVersionFile.getLength() + "");
            msgUpdateVer.setMd5(dmcCsunVersionFile.getMd5());
            msgUpdateVer.setEventId(DateUtil.getDateStr(new Date()));
            msgUpdateVer.setFileUrl(fileUrl);    // 下载地址
            msgUpdateVer.setSn(dmcDevice.getDeviceNo());

            String msg = JSONObject.fromObject(msgUpdateVer).toString();

            // 保存床垫指令
            DmcCsunSleepCmd dmcCsunSleepCmd = new DmcCsunSleepCmd();
            dmcCsunSleepCmd.setContent(msg);
            dmcCsunSleepCmd.setDeviceId(dmcDevice.getId());
            dmcCsunSleepCmd.setDeviceNo(dmcDevice.getDeviceNo());
            dmcCsunSleepCmd.setEventId(msgUpdateVer.getEventId());
            dmcCsunSleepCmd.setMsgType(msgUpdateVer.getMsgType());
            dmcCsunSleepCmd.setQos(qos);
            dmcCsunSleepCmdMapper.saveOrUpdate(dmcCsunSleepCmd);

            // 保存床垫指令记录
            DmcCsunSleepCmdHi dmcCsunSleepCmdHi = new DmcCsunSleepCmdHi();
            dmcCsunSleepCmdHi.setContent(msg);
            dmcCsunSleepCmdHi.setDeviceId(dmcDevice.getId());
            dmcCsunSleepCmdHi.setDeviceNo(dmcDevice.getDeviceNo());
            dmcCsunSleepCmdHi.setEventId(msgUpdateVer.getEventId());
            dmcCsunSleepCmdHi.setMsgType(msgUpdateVer.getMsgType());
            dmcCsunSleepCmdHi.setQos(qos);
            dmcCsunSleepCmdHiMapper.insertSelective(dmcCsunSleepCmdHi);

            sleepMessageGateway.sendToMqtt(dmcDevice.getDeviceNo(), qos, msg);
        }
    }


    public void sendUpdateVerMsg(List<DmcDevice> dmcDeviceList, VersionSetVo set) {

        int qos = 0;    // 消息质量
        List<DmcCsunVersionSetDevice> dmcCsunVersionSetDeviceList = org.python.google.common.collect.Lists.newArrayList();

        for (DmcDevice dmcDevice : dmcDeviceList) {

            MsgUpdateVer msgUpdateVer = new MsgUpdateVer();
            msgUpdateVer.setFileType(set.getFileType() + "");
            msgUpdateVer.setFileVersion(set.getFileVersion());
            msgUpdateVer.setLength(set.getLength() + "");
            msgUpdateVer.setMd5(set.getMd5());
            msgUpdateVer.setEventId(WatchUtils.getRandomString(12).toUpperCase());
            msgUpdateVer.setFileUrl(set.getFileUrl());    // 下载地址
            msgUpdateVer.setSn(dmcDevice.getDeviceNo());

            String msg = JSONObject.fromObject(msgUpdateVer).toString();

            // 保存床垫指令
            DmcCsunSleepCmd dmcCsunSleepCmd = new DmcCsunSleepCmd();
            dmcCsunSleepCmd.setContent(msg);
            dmcCsunSleepCmd.setDeviceId(dmcDevice.getId());
            dmcCsunSleepCmd.setDeviceNo(dmcDevice.getDeviceNo());
            dmcCsunSleepCmd.setEventId(msgUpdateVer.getEventId());
            dmcCsunSleepCmd.setMsgType(msgUpdateVer.getMsgType());
            dmcCsunSleepCmd.setQos(qos);
            dmcCsunSleepCmdMapper.saveOrUpdate(dmcCsunSleepCmd);

            // 保存床垫指令记录
            DmcCsunSleepCmdHi dmcCsunSleepCmdHi = new DmcCsunSleepCmdHi();
            dmcCsunSleepCmdHi.setContent(msg);
            dmcCsunSleepCmdHi.setDeviceId(dmcDevice.getId());
            dmcCsunSleepCmdHi.setDeviceNo(dmcDevice.getDeviceNo());
            dmcCsunSleepCmdHi.setEventId(msgUpdateVer.getEventId());
            dmcCsunSleepCmdHi.setMsgType(msgUpdateVer.getMsgType());
            dmcCsunSleepCmdHi.setQos(qos);
            dmcCsunSleepCmdHiMapper.insertSelective(dmcCsunSleepCmdHi);

            sleepMessageGateway.sendToMqtt(dmcDevice.getDeviceNo(), qos, msg);

            /**
             * @date    2020-05-13
             * @author  chisj
             * @desc    更新设备升级状态
             */
            DmcCsunVersionSetDevice record = new DmcCsunVersionSetDevice();
            record.setVersionSetId(set.getId());
            record.setDeviceId(dmcDevice.getId());
            record.setStat(DeviceConstant.VERSION_SET_CMD_CREATE);
            record.setTaskId(msgUpdateVer.getEventId());
            dmcCsunVersionSetDeviceList.add(record);
        }

        dmcCsunVersionSetDeviceService.batchUpdate(dmcCsunVersionSetDeviceList);
    }

    public void sendDataMsg(String deviceNo, String state) {

        int qos = 0;    // 消息质量

        DmcDevice dmcDevice = dmcDeviceService.queryDeviceByNumber(deviceNo);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100614003);
        }

        MsgSendData msgSendData = new MsgSendData();
        msgSendData.setMattessId(deviceNo);
        msgSendData.setState(state);
        msgSendData.setEventId(WatchUtils.getRandomString(12).toUpperCase());

        String msg = JSONObject.fromObject(msgSendData).toString();

        // 保存床垫指令
        DmcCsunSleepCmd dmcCsunSleepCmd = new DmcCsunSleepCmd();
        dmcCsunSleepCmd.setContent(msg);
        dmcCsunSleepCmd.setDeviceId(dmcDevice.getId());
        dmcCsunSleepCmd.setDeviceNo(dmcDevice.getDeviceNo());
        dmcCsunSleepCmd.setEventId(msgSendData.getEventId());
        dmcCsunSleepCmd.setMsgType(msgSendData.getMsgType());
        dmcCsunSleepCmd.setQos(qos);
        dmcCsunSleepCmdMapper.saveOrUpdate(dmcCsunSleepCmd);

        // 保存床垫指令记录
        DmcCsunSleepCmdHi dmcCsunSleepCmdHi = new DmcCsunSleepCmdHi();
        dmcCsunSleepCmdHi.setContent(msg);
        dmcCsunSleepCmdHi.setDeviceId(dmcDevice.getId());
        dmcCsunSleepCmdHi.setDeviceNo(dmcDevice.getDeviceNo());
        dmcCsunSleepCmdHi.setEventId(msgSendData.getEventId());
        dmcCsunSleepCmdHi.setMsgType(msgSendData.getMsgType());
        dmcCsunSleepCmdHi.setQos(qos);
        dmcCsunSleepCmdHiMapper.insertSelective(dmcCsunSleepCmdHi);

        sleepMessageGateway.sendToMqtt(dmcDevice.getDeviceNo(), qos, msg);

    }

    public void sendDataMsg(DmcDevice dmcDevice, String state) {

        int qos = 0;    // 消息质量

        MsgSendData msgSendData = new MsgSendData();
        msgSendData.setMattessId(dmcDevice.getDeviceNo());
        msgSendData.setState(state);
        msgSendData.setEventId(WatchUtils.getRandomString(12).toUpperCase());

        String msg = JSONObject.fromObject(msgSendData).toString();

        // 保存床垫指令
        DmcCsunSleepCmd dmcCsunSleepCmd = new DmcCsunSleepCmd();
        dmcCsunSleepCmd.setContent(msg);
        dmcCsunSleepCmd.setDeviceId(dmcDevice.getId());
        dmcCsunSleepCmd.setDeviceNo(dmcDevice.getDeviceNo());
        dmcCsunSleepCmd.setEventId(msgSendData.getEventId());
        dmcCsunSleepCmd.setMsgType(msgSendData.getMsgType());
        dmcCsunSleepCmd.setQos(qos);
        dmcCsunSleepCmdMapper.saveOrUpdate(dmcCsunSleepCmd);

        // 保存床垫指令记录
        DmcCsunSleepCmdHi dmcCsunSleepCmdHi = new DmcCsunSleepCmdHi();
        dmcCsunSleepCmdHi.setContent(msg);
        dmcCsunSleepCmdHi.setDeviceId(dmcDevice.getId());
        dmcCsunSleepCmdHi.setDeviceNo(dmcDevice.getDeviceNo());
        dmcCsunSleepCmdHi.setEventId(msgSendData.getEventId());
        dmcCsunSleepCmdHi.setMsgType(msgSendData.getMsgType());
        dmcCsunSleepCmdHi.setQos(qos);
        dmcCsunSleepCmdHiMapper.insertSelective(dmcCsunSleepCmdHi);

        sleepMessageGateway.sendToMqtt(dmcDevice.getDeviceNo(), qos, msg);

    }
}
